﻿using Api.ContentProvider;
using Api.Model;
using Core.Infrastructure.AnimationService;
using Core.Infrastructure.Constants;
using Core.Infrastructure.EventArgs;
using Core.Infrastructure.ScreenManager;
using Core.Infrastructure.Settings;
using Core.Infrastructure.ViewModelBase;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Unity;
using ThemeLoader;

namespace Core.ScreenStore.ViewModel
{
    public class ArticleDetailsViewModel : ViewModelBase, INavigable<Article>
    {
        private Article article;
        private IContentProvider provider;
        private IResourceProvider resourceProvider;

        public Article Article
        {
            get
            {
                return article;
            }
            set
            {
                article = value;
                base.RaisePropertyChanged(() => this.Article);
            }
        }

        public IContentProvider ContentProvider 
		{
            get 
            {
                return provider;
            }
            set
            {
                provider = value;
                base.RaisePropertyChanged(() => this.ContentProvider);
            }
        }

        public IResourceProvider ResourceProvider
        {
            get
            {
                return resourceProvider;
            }
            set
            {
                resourceProvider = value;
                base.RaisePropertyChanged(() => this.ResourceProvider);
            }
        }

        public DelegateCommand<Tag> SelectTagCommand { get; private set; }

        public ArticleDetailsViewModel(IEventAggregator eventAggregator,
                                IAnimationService animationService,
                                IUnityContainer container,
                                ISettings settings,
                                IContentProvider provider,
                                IResourceProvider resourceProvider)
            : base(eventAggregator, animationService, container, settings)
        {
            AnimationService = animationService;
            Settings = settings;
            Loaded = new DelegateCommand<object>(OnLoad);
            Unloaded = new DelegateCommand<object>(OnUnload);
            this.ContentProvider = provider;
            this.ResourceProvider = resourceProvider;
            SelectTagCommand = new DelegateCommand<Tag>(tag => SelectTag(tag),
                                                        tag => CanSelectTag(tag));
        }

        private bool CanSelectTag(Tag tag)
        {
            return true;
        }

        private void SelectTag(Tag tag)
        {
            var screenEventArgs = new ScreenEventArgs
            {
                ScreenType = ScreenType.ArticlesScreen,
                RegionName = RegionConstants.RegionMainArea,
                PerformAnimation = true,
                NavigationArgs = tag
            };
            this.EventAggregator.GetEvent<ScreenActivateEvent>().Publish(screenEventArgs);
        }   

        public override bool IsActive { get; set; }

        public override void OnUnload(object obj)
        {
            this.IsActive = false;
        }

        public override void OnLoad(object obj)
        {
            this.IsActive = true;
        }

        public void OnNavigate(Article args)
        {
            Article = args;
        }
    }
}
